
package com.netease.xone.activity;

import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.X1LoadingAdapterViewBase.OnLoadingListener;
import com.netease.xone.R;
import com.netease.xone.widget.listview.LoadingListView;

public class test_RefreshList extends ActivityX1Base {

    public static void startActivity(Context context) {
        Intent i = new Intent(context, test_RefreshList.class);
        context.startActivity(i);
    }

    LoadingListView mRefreshList;
    TestAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity_refresh_list);
        mRefreshList = (LoadingListView)findViewById(R.id.test_refresh_list);

        mAdapter = new TestAdapter(this);
        mRefreshList.setAdapter(mAdapter);

        TextView tv = new TextView(this);
        tv.setBackgroundColor(Color.YELLOW);
        tv.setText("header");
        mRefreshList.getRefreshableView().addHeaderView(tv);
        // 设置Loading时的回调
        mRefreshList.setOnLoadingListener(new OnLoadingListener() {

            @Override
            public void onRefreshing() {
                new RefreshTask().execute();
            }

            @Override
            public void onLoading() {
                new LoadingTask().execute();
            }

            @Override
            public void onLoadingMore() {
                new LoadingMoreTask().execute();
            }
        });
        if (mRefreshList.getLoadingState() == LoadingListView.STATE_IDLE) {
            mRefreshList.load();
        }
    }

    private class LoadingTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mAdapter.mCount = 60;
            mAdapter.notifyDataSetChanged();
            mRefreshList.onLoadingComplete(true);
        }
    }

    private class RefreshTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            String label = new Date().toString();
            mRefreshList.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            mAdapter.mCount = 60;
            mAdapter.mColor += 1;
            mAdapter.notifyDataSetChanged();
            mRefreshList.onRefreshComplete();
            mRefreshList.setHasMore(true);
        }
    }

    private class LoadingMoreTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mAdapter.mCount += 20;
            mAdapter.notifyDataSetChanged();
            if (mAdapter.mCount < 100) {
                mRefreshList.onLoadingComplete(true);
            } else {
                mRefreshList.onLoadingComplete(false);
            }
        }
    }

    /**
     * 使用count模拟数据
     */
    class TestAdapter extends BaseAdapter {

        private Context mContext;
        int mCount = 0;
        int mColor = 0;

        public TestAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return mCount;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new TextView(mContext);
            }
            ((TextView)convertView).setText("position:" + getItem(position));
            if (mColor == 0) {
                convertView.setBackgroundColor(Color.rgb(position * 8, 0, 0));
            } else if (mColor == 1) {
                convertView.setBackgroundColor(Color.rgb(0, position * 8, 0));
            } else {
                convertView.setBackgroundColor(Color.rgb(0, 0, position * 8));
            }
            return convertView;
        }

    }
}
